import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { resolve } from 'path';
import { viteStaticCopy } from 'vite-plugin-static-copy'
import { defineConfig, loadEnv, ConfigEnv } from 'vite'

const itkConfig = resolve(__dirname, 'src', 'itkConfig.js')
const alias: Record<string, string> = {
	'/@': resolve(__dirname, '.', './src/'),
	'./ itkConfig.js': itkConfig,
	'../../itkConfig.js': itkConfig
};

const viteConfig = defineConfig((mode: ConfigEnv) => {
	const env = loadEnv(mode.mode, process.cwd());
	return {
		plugins: [
			vue(),
			vueJsx(),
			viteStaticCopy({
				targets: [
					{ src: 'node_modules/itk-wasm/dist/web-workers/*', dest: 'dist/itk/web-workers' },
					{
						src: 'node_modules/itk-image-io/*',
						dest: 'dist/itk/image-io',
					},
					{
						src: 'node_modules/itk-mesh-io/*',
						dest: 'dist/itk/mesh-io',
						rename: 'mesh-io'
					}
				]
			})
		],
		root: process.cwd(),
		resolve: { alias },
		base: './',
		define: {
			'process.env': {
				VITE_API_URL: env.VITE_API_URL,
				VITE_API_ENV: env.VITE_API_ENV
			}
		},
		server: {
			host: '0.0.0.0',
			port: env.VITE_PORT as unknown as number,
			open: env.VITE_OPEN,
			proxy: {
				// '/api': {
				// 	target: env.VITE_API_URL,
				// 	ws: true,
				// 	changeOrigin: true,
				// 	rewrite: (path) => path.replace(/^\/api/, ''),
				// },
				// '/_AMapService/': {
				// 	target: 'https://restapi.amap.com/',
				// 	ws: true,
				// 	changeOrigin: true,
				// 	// rewrite: (path) => path.replace(/^\/apiOut/, ''),
				// },
			},
		},
		build: {
			// assetsDir: "./",
			// outDir: 'dist',
			// sourcemap: false,
			// chunkSizeWarningLimit: 1500,
			// rollupOptions: {
			// 	output: {
			// 		entryFileNames: `assets/[name].${new Date().getTime()}.js`,
			// 		chunkFileNames: `assets/[name].${new Date().getTime()}.js`,
			// 		assetFileNames: `assets/[name].${new Date().getTime()}.[ext]`,
			// 		compact: true,
			// 		manualChunks: {
			// 			vue: ['vue', 'vue-router', 'pinia'],
			// 			echarts: ['echarts'],
			// 		},
			// 	},
			// },
		}
		,
		css: { preprocessorOptions: { css: { charset: false } } },
	} as any;
});

export default viteConfig;
